package com.daxia.bumall.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daxia.bumall.dao.TimelineLikeDAO;
import com.daxia.bumall.dto.TimelineLikeDTO;
import com.daxia.bumall.model.Timeline;
import com.daxia.bumall.model.TimelineLike;
import com.daxia.bumall.model.User;
import com.daxia.bumall.support.Page;
import com.daxia.bumall.util.BeanMapper;

@Service
public class TimelineLikeService {
	
	@Autowired
	private TimelineLikeDAO timelineLikeDAO;
	
	public List<TimelineLikeDTO> find(TimelineLikeDTO query, Page page) {
		List<TimelineLike> models = timelineLikeDAO.find(query, page);
		List<TimelineLikeDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	public List<TimelineLikeDTO> findAll() {
		return this.find(new TimelineLikeDTO(), null);
	}
	
	public List<TimelineLikeDTO> find(int count) {
		Page page = new Page();
		page.setPage(1);
		page.setPageSize(count);
		return this.find(new TimelineLikeDTO(), page);
	}
	
	public Long create(TimelineLikeDTO dto) {
		TimelineLike model = new TimelineLike();
		toModel(model, dto);
		return timelineLikeDAO.create(model);
	}
	
	public TimelineLikeDTO load(Long id) {
	    TimelineLike model = timelineLikeDAO.load(id);
	    return toDTO(model);
    }

	public void updateAllFields(TimelineLikeDTO dto) {
		TimelineLike model = timelineLikeDAO.load(dto.getId());
		toModel(model, dto);
		timelineLikeDAO.update(model);
    }

	public void deleteByIds(Long[] ids) {
		if (ids != null) {
			for (Long id : ids) {
				timelineLikeDAO.deleteById(id);
			}
		}
    }
	
	public void deleteById(Long id) {
		this.deleteByIds(new Long[] {id});
	}

	public TimelineLikeDTO findOne(TimelineLikeDTO query) {
		TimelineLike model = timelineLikeDAO.findOne(query);
		return toDTO(model);
	}
	
	private List<TimelineLikeDTO> toDTOs(List<TimelineLike> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<TimelineLikeDTO>(0);
		}
		List<TimelineLikeDTO> dtos = new ArrayList<TimelineLikeDTO>(models.size());
		for (TimelineLike model : models) {
	        TimelineLikeDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
    
    private TimelineLikeDTO toDTO(TimelineLike model) {
		if (model == null) {
			return null;
		}
		TimelineLikeDTO dto = BeanMapper.map(model, TimelineLikeDTO.class);
		
		return dto;
	}
	
	private void toModel(TimelineLike model, TimelineLikeDTO dto) {
		BeanMapper.copy(dto, model);
    }
	
	@SuppressWarnings("unused")
	private List<TimelineLike> toModels(List<TimelineLikeDTO> dtos) {
		if (CollectionUtils.isEmpty(dtos)) {
			return new ArrayList<TimelineLike>(0);
		}
		List<TimelineLike> models = new ArrayList<TimelineLike>(dtos.size());
		for (TimelineLikeDTO dto : dtos) {
	        TimelineLike model = new TimelineLike();
	        toModel(model, dto);
	        models.add(model);
        }
		return models;
	}

	public TimelineLikeDTO findByUserIdAndTimelineId(Long userId, Long timelineId) {
		
		TimelineLikeDTO dto = new TimelineLikeDTO();
		User user = new User();
		Timeline timeline = new Timeline();
		user.setId(userId);
		timeline.setId(timelineId);
		dto.setUser(user);
		dto.setTimeline(timeline);
		TimelineLike timelineLike = timelineLikeDAO.findOne(dto);
		return toDTO(timelineLike);
	}
}
